//
//  OneDiseaseTypeViewController.m
//  QuickCure
//
//  Created by 王震 on 16/9/12.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "OneDiseaseTypeViewController.h"
#import <Masonry.h>
#import "SelectView.h"
#import "ViewController.h"
#import "DiseaseTableViewController.h"
#import "FocusDoctorViewController.h"
#import "NetWorkTool.h"
#import <SVProgressHUD.h>

@interface OneDiseaseTypeViewController ()<UIWebViewDelegate>

@property(nonatomic,strong) UILabel *namePromptLabel;
@property(nonatomic,strong) UILabel *typePromptLabel;
@property(nonatomic,strong) UILabel *nameLabel;
@property(nonatomic,strong) UILabel *typeLabel;
@property(nonatomic,strong) SelectView *typeSelectView;
@property(nonatomic,strong) SelectView *complicationView;
@property(nonatomic,strong) SelectView *isDiagnose;
@property(nonatomic,strong) SelectView *isTreated;
@property(nonatomic,strong) SelectView *checkTreatType;
@property(nonatomic,strong) UILabel *matchLabel;
@property(nonatomic,strong) UIImageView *headImageView;
@property(nonatomic,strong) UIButton *application;

@end

@implementation OneDiseaseTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithRed:0.902 green:0.902 blue:0.9333 alpha:1.0]];

    
    
    if (self.selectIndex == 5) {
        
        //使用webView加载网络页面
        [self setWebView];
        
    }else{
        
        //创建子控件
        [self creatSubViews];
        //设置子控件frame
        [self setSubViewsFrame];
        //设置子控件数据及显示
        [self setSubViewsData];
        //获取匹配到的医生数量
        [self getNumberOfDoctor];
        
    }
}


#pragma mark - 使用webView加载网络页面
-(void)setWebView{
    
    //公益项目网址
    //http://202.106.210.115:18080/hyde-pluto-h360/nt/appEntrance.htm
    
    UIWebView *gongYi = [[UIWebView alloc] initWithFrame:self.view.bounds];

    NSURL *url = [NSURL URLWithString:@"http://202.106.210.115:18080/hyde-pluto-h360/nt/appEntrance.htm"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [gongYi loadRequest:request];
    [self.view addSubview:gongYi];
}



#pragma mark - 创建子控件
-(void)creatSubViews{
    
    [self.view addSubview:self.namePromptLabel];
    [self.view addSubview:self.typePromptLabel];
    
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.typeLabel];
    
    [self.view addSubview:self.typeSelectView];
    
    [self.view addSubview:self.complicationView];
    
    [self.view addSubview:self.isDiagnose];
    
    [self.view addSubview:self.isTreated];
    
    [self.view addSubview:self.checkTreatType];
    
    [self.view addSubview:self.application];
    
    [self.view addSubview:self.headImageView];
    
    [self.view addSubview:self.matchLabel];
    
    
}

#pragma mark - 设置子控件frame
-(void)setSubViewsFrame{
    
    [self.namePromptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.leading.equalTo(self.view).offset(17);
       
    }];
    [self.typePromptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.namePromptLabel.mas_bottom).offset(10);
        make.leading.equalTo(self.namePromptLabel);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.namePromptLabel);
        make.leading.equalTo(self.namePromptLabel.mas_trailing).offset(20);
    }];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typePromptLabel);
        make.leading.equalTo(self.typePromptLabel.mas_trailing).offset(20);
    }];
    
    //设置四个typeSelectView的约束
    [self.typeSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeLabel.mas_bottom).offset(20);
        make.left.equalTo(self.view).offset(17);
        make.width.equalTo(@(341));
        make.height.equalTo(@(34));
    }];
    
    [self.complicationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeSelectView.mas_bottom).offset(15);
        make.leading.trailing.equalTo(self.typeSelectView);
        make.height.equalTo(@(34));
    }];
    
    [self.isDiagnose mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.complicationView.mas_bottom).offset(15);
        make.leading.trailing.equalTo(self.typeSelectView);
        make.height.equalTo(@(34));
    }];
    
    [self.isTreated mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.isDiagnose.mas_bottom).offset(15);
        make.leading.trailing.equalTo(self.typeSelectView);
        make.height.equalTo(@(34));
    }];
    
    [self.checkTreatType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.isTreated.mas_bottom).offset(15);
        make.leading.trailing.equalTo(self.typeSelectView);
        make.height.equalTo(@(34));
    }];
    
    [self.application mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.typeSelectView);
        make.bottom.equalTo(self.view).offset(-20);
        make.height.equalTo(@(40));
    }];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.application.mas_top).offset(-30);
        make.width.height.equalTo(@(70));
    }];
    
    
    [self.matchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.headImageView.mas_top).offset(-30);
    }];
    
}

#pragma mark - 设置子控件数据及显示
-(void)setSubViewsData{
    
    [self.namePromptLabel setText:@"用户信息:"];
    [self.typePromptLabel setText:@"疾病类型:"];
    [self.nameLabel setText:@"王二狗1000099"];
    [self.typeLabel setText:self.title];
    
    UILabel *currentLabel = [UILabel appearance];
    [currentLabel setFont:[UIFont systemFontOfSize:18]];
    
    [self.typeSelectView setTag:11];
    [self.typeSelectView setImageWithText:@"请选择疾病细分"];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.typeSelectView addGestureRecognizer:tap1];
    
    [self.complicationView setTag:22];
    [self.complicationView setImageWithText:@"请选择并发症(可多选)"];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.complicationView addGestureRecognizer:tap2];
    
    [self.isDiagnose setButtonsWithNomalText:@"是否确证" andText:@"已确诊" andText:@"症状意思"];
    [self.isTreated setButtonsWithNomalText:@"是否接受过治疗" andText:@"接受过" andText:@"未接受过"];
    
    __weak typeof(self) weakSelf = self;
    self.isTreated.expentBlock = ^(BOOL isExpent){
        __strong typeof(weakSelf) strongSelf = weakSelf;
        
        [strongSelf.checkTreatType setHidden:isExpent];
        [strongSelf.view layoutSubviews];
    };
    [self.checkTreatType setTag:33];
    [self.checkTreatType setImageWithText:@"请选择治疗方式"];
    [self.checkTreatType setHidden:YES];
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.checkTreatType addGestureRecognizer:tap3];
    
    [self.application setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    [self.application setTitle:@"就医申请" forState:UIControlStateNormal];
    [self.application addTarget:self action:@selector(gotoChengJianjian) forControlEvents:UIControlEventTouchUpInside];
    
    self.headImageView.image = [UIImage imageNamed:@"illness_img_person.png"];

    
//    [self.matchLabel setText:@"为您匹配到: 42位医生"];
}

#pragma mark - 匹配医生数量
-(void)getNumberOfDoctor{
    
    //http://iosapi.itcast.cn/matchedDoctorCount.json.php
    //参数
    //{"ci1_id":1,"ci2_id":3,"ci3_id":3,"diagnosis_type":0,"is_confirmed":1,"user_id":1000089, "has_diagnosis":2}
    NSString *urlString = @"http://iosapi.itcast.cn/matchedDoctorCount.json.php";
    NSDictionary *dict = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"is_confirmed":@1,@"user_id":@1000089, @"has_diagnosis":@2};
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:dict successBloock:^(id responseObject) {
        
        NSDictionary *countDict = responseObject[@"data"];
        NSString *docterCount = countDict[@"doctor_count"];
        [self.matchLabel setText:[NSString stringWithFormat:@"为您匹配到: %@位医生",docterCount]];
        [self.view layoutSubviews];
        
    } failureBlock:^(NSError *error) {
        

    }];
}

#pragma mark -三个选项View的点击方法
-(void)tap:(UITapGestureRecognizer *)tap{
    
    DiseaseTableViewController *diseaseTVC = [[DiseaseTableViewController alloc] init];
    [self.navigationController pushViewController:diseaseTVC animated:YES];
 
    diseaseTVC.currentIndex = self.selectIndex;
    diseaseTVC.currentSelectView = tap.view;

    diseaseTVC.checkBlock = ^(NSString *str){
        
        SelectView *view = (SelectView *)tap.view;
        view.titleLabel.text = str;
        [view.titleLabel setTextColor:[UIColor blackColor]];
        
    };
    
}


#pragma mark - 控件的懒加载
-(UILabel *)namePromptLabel{
    if (nil == _namePromptLabel) {
        _namePromptLabel = [[UILabel alloc] init];
    }
    return _namePromptLabel;
}

-(UILabel *)typePromptLabel{
    if (nil == _typePromptLabel) {
        _typePromptLabel = [[UILabel alloc] init];
    }
    return _typePromptLabel;
}

-(UILabel *)nameLabel{
    if (nil == _nameLabel) {
        _nameLabel = [[UILabel alloc] init];
    }
    return _nameLabel;
}

-(UILabel *)typeLabel{
    if (nil == _typeLabel) {
        _typeLabel = [[UILabel alloc] init];
    }
    return _typeLabel;
}

-(SelectView *)typeSelectView{
    if (nil == _typeSelectView) {
        _typeSelectView = [[SelectView alloc] init];
    }
    return _typeSelectView;
}

-(SelectView *)complicationView{
    if (nil == _complicationView) {
        _complicationView = [[SelectView alloc] init];
    }
    return _complicationView;
}

-(SelectView *)isDiagnose{
    if (nil == _isDiagnose) {
        _isDiagnose = [[SelectView alloc] init];
    }
    return _isDiagnose;
}

-(SelectView *)isTreated{
    if (nil == _isTreated) {
        _isTreated = [[SelectView alloc] init];
    }
    return _isTreated;
}

-(SelectView *)checkTreatType{
    if (nil == _checkTreatType) {
        _checkTreatType = [[SelectView alloc] init];
    }
    return _checkTreatType;
}

-(UIButton *)application{
    if (nil == _application) {
        _application = [[UIButton alloc] init];
    }
    return _application;
}

-(UIImageView *)headImageView{
    if (nil == _headImageView) {
        _headImageView = [[UIImageView alloc] init];
    }
    return _headImageView;
}

-(UILabel *)matchLabel{
    if (nil == _matchLabel) {
        _matchLabel = [[UILabel alloc] init];
    }
    return _matchLabel;
}

#pragma mark - push程尖尖
-(void)gotoChengJianjian{
    
    FocusDoctorViewController *chengjj = [[FocusDoctorViewController alloc] init];
    [self.navigationController pushViewController:chengjj animated:YES];
}

@end













